package com.starnet.yarnmonitor.yarn.controller;

import com.starnet.yarnmonitor.yarn.handler.ApiException;
import com.starnet.yarnmonitor.yarn.service.ClusterService;
import com.starnet.yarnmonitor.yarn.vo.AppVO;
import com.starnet.yarnmonitor.yarn.vo.NodeVO;
import com.starnet.yarnmonitor.yarn.vo.result.Result;
import com.starnet.yarnmonitor.yarn.utils.PageUtils;
import com.starnet.yarnmonitor.yarn.vo.MetricsVO;
import com.starnet.yarnmonitor.yarn.vo.QueueVO;
import com.starnet.yarnmonitor.yarn.vo.result.PageResult;
import com.starnet.yarnmonitor.yarn.vo.result.PageVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
@Log4j
@RestController()
@RequestMapping("/yarn/cluster")
public class ClusterController {
    @Autowired
    ClusterService clusterService;

    @ApiOperation(value = "获取所有节点情况")

    @GetMapping("/nodes")
    public PageResult getAllNodes(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) throws IOException {
        List<NodeVO> allNodes = clusterService.getAllNodes(cur, size);
        List pagedRes = PageUtils.getPagedList(allNodes, cur, size);
        PageVO pageVoFromList = PageUtils.getPageVoFromList(allNodes, cur, size);
        return PageResult.success(pagedRes, pageVoFromList);
    }

    @ApiOperation(value = "获取集群资源")
    @GetMapping("/info")
    public Result getClusterInfo() throws IOException {
        MetricsVO metrics = null;
            metrics = clusterService.getClusterInfo();
        return Result.success(metrics);
    }

    @ApiOperation(value = "获取所有队列")
    @GetMapping("/queues")
    public PageResult getPagedQueues(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) throws IOException, YarnException {
        List<QueueVO> allQueues = clusterService.getAllQueues(cur, size);
        //分页后的返回结果
        List pagedRes = PageUtils.getPagedList(allQueues, cur, size);
        PageVO pageVoFromList = PageUtils.getPageVoFromList(allQueues, cur, size);
        return PageResult.success(pagedRes, pageVoFromList);

    }

    @ApiOperation(value = "获取所有应用情况")
    @GetMapping("/apps")
    public PageResult getPagedApps(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) throws IOException, YarnException {
        List<AppVO> allApps = clusterService.getAllApps(cur, size);
        //分页后的返回结果
        if (allApps==null){throw new ApiException("Yarn中暂无已运行app");
        }
        List pagedRes = PageUtils.getPagedList(allApps, cur, size);
        PageVO pageVoFromList = PageUtils.getPageVoFromList(allApps, cur, size);
        return PageResult.success(pagedRes, pageVoFromList);
    }


    @ApiOperation(value = "根据applicationId获取应用情况")
    @GetMapping("/apps/{appId}")
    public Result GetAllApps(@PathVariable(value = "appId", required = true) String applicationId) throws IOException, YarnException {
        AppVO app = clusterService.getAppById(applicationId);
        return Result.success(app);
    }

    @ApiOperation(value = "根据id获取应用日志")
    @GetMapping("/apps/logs/{appId}")
    @ResponseBody
    public void getAppById(@PathVariable(value = "appId", required = false) String applicationId, HttpServletResponse response) throws IOException {
            clusterService.getLogsByAppId(response, applicationId);
        return ;
    }


}
